<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>子文档聚合 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/children-agg.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/children-agg.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">子文档聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="has-parent.html">« 通过父文档查询子文档</a>
</span>
<span class="next">
<a href="grandparents.html">祖辈与孙辈关系 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="children-agg"></a>子文档聚合<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/60_Children_agg.asciidoc">edit</a>
</h2>
</div></div></div>
<p>父-子关系支持
<a href="http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html" class="ulink" target="_top"><code class="literal">children</code>聚合</a>，它直接模拟了<a class="xref" href="nested-aggregation.html" title="嵌套聚合">嵌套聚合</a>中的<code class="literal">nested</code>聚合。但是，对于父文档的聚合查询是不支持的（和<code class="literal">reverse_nested</code>类似）。</p>
<p>我们通过下面的例子来演示按照国家维度查看最受雇员欢迎的业余爱好：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/branch/_search
{
  "size" : 0,
  "aggs": {
    "country": {
      "terms": { <a id="CO282-1"></a><i class="conum" data-value="1"></i>
        "field": "country"
      },
      "aggs": {
        "employees": {
          "children": { <a id="CO282-2"></a><i class="conum" data-value="2"></i>
            "type": "employee"
          },
          "aggs": {
            "hobby": {
              "terms": { <a id="CO282-3"></a><i class="conum" data-value="3"></i>
                "field": "hobby"
              }
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO282-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">country</code>是<code class="word">branch</code>文档的一个字段。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO282-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>子文档聚合通过<code class="literal">type</code>为<code class="word">employee</code>的子文档与其父文档关联在一起。
</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO282-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">hobby</code>是<code class="word">employee</code>子文档的一个字段。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="has-parent.html">« 通过父文档查询子文档</a>
</span>
<span class="next">
<a href="grandparents.html">祖辈与孙辈关系 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>